package pers.qianyu.month_202101.date_20210118;

import org.junit.*;

/**
 * 165. 比较版本号
 * https://leetcode-cn.com/problems/compare-version-numbers/
 *
 * @author mizzle rain
 * @date 2021-01-18 11:14
 */
public class CompareVersion {
    public int compareVersion(String v1, String v2) {
        String[] s1 = v1.split("\\.");
        String[] s2 = v2.split("\\.");
        int len1 = s1.length, len2 = s2.length;
        int cur = 0;
        while (cur < len1 && cur < len2) {
            int num1 = Integer.parseInt(s1[cur]);
            int num2 = Integer.parseInt(s2[cur]);
            if (num1 < num2) {
                return -1;
            } else if (num1 > num2) {
                return 1;
            } else {
                cur++;
            }
        }
        while (cur < len1) {
            if (Integer.parseInt(s1[cur]) > 0) return 1;
            cur++;
        }
        while (cur < len2) {
            if (Integer.parseInt(s2[cur]) > 0) return -1;
            cur++;
        }
        return 0;
    }

    @Test
    public void test1() {
        int result = new CompareVersion().compareVersion("1", "1.1");
        System.out.println("result = " + result);
    }
}
